//
//  UIBarButtonItem+IBExtension.swift
//  weibohhhh
//
//  Created by fersh on 16/1/3.
//  Copyright © 2016年 fersh. All rights reserved.
//

import UIKit
/**
 *  为UIBarButtonItem添加一个分类,自定义一个初始化的方法,自己设定参数,按照需要传入参数
 */
extension UIBarButtonItem {
    
    convenience init(imageName: String? = nil,titel: String? = nil, target: AnyObject?, action: Selector) {
        
        self.init()
        //初始化一个按钮
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        if  imageName != nil {
            //设置图片不同状态下的显示
            button.setImage(UIImage(named: imageName!), forState: UIControlState.Normal)
            button.setImage(UIImage(named: "\(imageName!)_highlighted"), forState: UIControlState.Highlighted)
    }
        
        if titel != nil {
            
            //设置字体的样式
            button.setTitle(titel, forState: UIControlState.Normal)
            button.titleLabel? .font = UIFont.systemFontOfSize(15)
            button.setTitleColor(UIColor(white: 80/255, alpha: 1), forState: UIControlState.Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
        }
        button.sizeToFit()
        customView = button
    }
    
}
